from app.models import News
from app import db
from exts import JsonConverter, BeanUtils


def news_add(news):
    '''
    添加新闻
    :param news:
    :return:
    '''
    o = JsonConverter.dict_to_object(news, News)
    db.session.add(o)
    db.session.commit()


def news_update(news):
    '''
    更新新闻
    :param news:
    :return:
    '''
    '''
    更新客户留言
    :param news:
    :return:
    '''
    o = News.query.get(news['id'])
    # 对象复制
    BeanUtils.copy(news, o)
    db.session.commit()


def news_delete(id):
    '''
    删除新闻
    :param id:
    :return:
    '''
    o = News.query.get(id)
    db.session.delete(o)
    db.session.commit()


def news_detail(id):
    '''
    查询新闻详情
    :param id:
    :return:
    '''
    return News.query.get(id)


def news_page(pageNum, pageSize):
    '''
    分页查询新闻
    :param params:
    :return:
    '''
    return News.query.paginate(page=pageNum, per_page=pageSize)
